from django import  template
from django.utils.safestring import mark_safe

from MallApp import models
from django.shortcuts import render, redirect
from django.db.models import Q
from django.db.models import F
from datetime import datetime, time
from itertools import chain
import  random
register=template.Library()

@register.inclusion_tag('MallApp/Inclusiontemplate/topbarMain.html')  #html片段模版  当前用户页头信息
def TopbarMain(phone):
    users = models.UserInfo.objects.filter(Q(Phone=phone))
    if len(users) <= 0:
        return {"user": ""}
    else:
        return {"user": users[0]}

@register.inclusion_tag('MallApp/Inclusiontemplate/topbarMain.html')  #html片段模版  当前用户页头信息
def TopbarMain2(phone):
    users = models.UserInfo.objects.filter(Q(Phone=phone))
    GameType=models.GameTypeDict.objects.all()
    if len(users) <= 0:
        return {"User": "","GameTypes":GameType}
    else:
        return {"User": users[0],"GameTypes":GameType}



@register.inclusion_tag('MallApp/Inclusiontemplate/RightSide.html')  #html片段模版 右侧网站推荐
def RightSideMenu():
    HotGameSites = models.HotGameSites.objects.all().order_by("-HotValue")
    return {"HotGameSites": HotGameSites}




@register.simple_tag() # 自定义标签 入参无限制  只能在 {% %} 中使用  显示标签
def TagsTransform_tag(strTags):
    Tags=strTags.split('|')
    return Tags


@register.inclusion_tag('MallApp/Inclusiontemplate/blogtags.html')  #html片段模版
def blogtags(strTags):
    Tags = strTags.split('|')
    return {'Tags':Tags}


@register.simple_tag() # 计算评论数
def IssueCommentCount(IssueReportNid):
    count=len(models.IssueComment.objects.filter(Q(IssueReportNid=IssueReportNid)))
    return count



@register.inclusion_tag('MallApp/Inclusiontemplate/luckNum.html') #幸运数字展示
#可变和不可变序列使用例子
def getLuckNum():
    KBList=[] #定义列表 可变序列
    BKBString="1|2|3|4|5|6|7|8|9|0" #字符串 不可变序列
    KBList.append("今天你的幸运数字是：")
    KBList.append("今天你需要避讳的数字是：")
    numList=BKBString.split('|')
    #随机显示内容
    index=random.randint(0,9)
    index2 = random.randint(0, 100)
    outstr=KBList[index2% 2]+str(numList[index])
    return {'outstr': outstr}








@register.inclusion_tag('MallApp/Inclusiontemplate/UserRecommend.html')  #html片段模版 个人推荐
def UserRecommend(UserID):
    if UserID=="":
        UserRecommendItems = models.IssueReportMain.objects.all().order_by("-HotValue")[:5]
        return {'UserRecommendItems':UserRecommendItems}
    #获取用户信息
    userInfo=models.UserInfo.objects.get(Phone=UserID)
    #获取用户喜爱数据
    userLoveData=models.UserGameTypeCount.objects.filter(Q(User=userInfo)).order_by("-HotValue")
    UserRecommendItems=models.IssueReportMain.objects.filter(Q(HotValue=-99))
    for rowdata in userLoveData:
        Gameinfos = models.IssueReportMain.objects.filter(Q(GameType=rowdata.GameType)).order_by("-HotValue")[:2]
        UserRecommendItems=chain(UserRecommendItems,Gameinfos)
        UserRecommendItems = [iterm for iterm in UserRecommendItems]
    if len(UserRecommendItems)<=0:
        UserRecommendItems=models.IssueReportMain.objects.all().order_by("-HotValue")[:5]
    return {'UserRecommendItems':UserRecommendItems}

@register.simple_tag() # 自定义标签 入参无限制  只能在 {% %} 中使用  显示标签
def SubTitle(title):
    subTitle=title
    if len(title)>12:
        subTitle=title[0:12]+"..."
    return subTitle